<?php
/**
 * SFKNews
 * 私房库 www.sifangku.com 教学项目
 *
 * @desc Model基类
 * @copyright 私房库 www.sifangku.com 保留所有版权
 * @author 孙胜利 www.sunshengli.com
 * @version 1.01
 *
 **/
namespace Common;
if(!defined('SITE')) exit('Access Denied');
class Model1 {
	protected $config;
	protected $db;
	protected $dbName;
	static protected $tableInfo;
	public function __construct(Array $config=null){
		if($config==null){
			if(defined('DB_HOST')){
				$this->config['host']=\DB_HOST;
			}
			if(defined('DB_USER')){
				$this->config['user']=\DB_USER;
			}
			if(defined('DB_PASSWORD')){
				$this->config['password']=\DB_PASSWORD;
			}
			if(defined('DB_DATABASE')){
				$this->config['database']=\DB_DATABASE;
			}
			if(defined('DB_PORT')){
				$this->config['port']=\DB_PORT;
			}
			if(defined('DB_CHARSET')){
				$this->config['charset']=\DB_CHARSET;
			}
		}else{
			$this->config=$config;
		}
	}
	public function getDB(){
		if(!isset($this->db)){
			$this->db=new DB($this->config);
		}
		return $this->db;
	}
	//获取当前连接的数据库服务器地址
	public function getDBHost(){
		if(isset($this->config['host'])){
			return $this->config['host'];
		}else{
			return 'localhost';
		}
	}
	//获取当前连接的数据库服务器端口号
	public function getDBPort(){
		if(isset($this->config['port'])){
			return $this->config['port'];
		}else{
			return 3306;
		}
	}
	//选择操作的数据库
	public function selectDB($dbname){
		if($this->getDB()->select_db($dbname)){
			$this->dbName=$dbname;
			return true;
		}else{
			return false;
		}
	}
	//获取当前操作的数据库名称
	public function getDBName(){
		if(isset($this->dbName)){
			return $this->dbName;
		}elseif(isset($this->config['database'])){
			return $this->config['database'];
		}else{
			$sql='select database()';
			$data=$this->getDB()->execute(array('sql'=>$sql));
			return $data[0]['database()'];
		}
	}
	//获取表信息
	public function getTableInfo($tableName){
		$sql="SHOW FULL COLUMNS FROM {$tableName}";
		$info=$this->getDB()->execute(array('sql'=>$sql));
		foreach ($info as $val){
			//字段
			$indexDBHost=$this->getDBHost().'_'.$this->getDBPort();
			$indexDBName=$this->getDBName();
			$indexTableName=$tableName;
			self::$tableInfo[$indexDBHost][$indexDBName][$indexTableName]['fields'][]=$val['Field'];
			//主键
			if($val['Key']=='PRI'){
				self::$tableInfo[$indexDBHost][$indexDBName][$indexTableName]['pk']=$val['Field'];
			}
			//注释
			self::$tableInfo[$indexDBHost][$indexDBName][$indexTableName]['fields_comment'][$val['Field']]=$val['Comment'];
			//字段类型（bind用）
			if(strpos($val['Type'],'int')!==false){
				self::$tableInfo[$indexDBHost][$indexDBName][$indexTableName]['fields_type'][$val['Field']]='i';
			}elseif(strpos($val['Type'],'float')!==false || strpos($val['Type'],'double'!==false)){
				self::$tableInfo[$indexDBHost][$indexDBName][$indexTableName]['fields_type'][$val['Field']]='d';
			}else{
				self::$tableInfo[$indexDBHost][$indexDBName][$indexTableName]['fields_type'][$val['Field']]='s';
			}
			//字段规则自动生成（入库时检测用）
			/*
			require
				什么时候字段可以不传
					允许为空
					自增
					有默认值
			number
				int
				float
				double
				decimal
			unique
				
			length
			*/
			if($val['Null']=='YES' || strpos($val['Extra'],'auto_increment')!==false || $val['Default']!=null){
				//self::$tableInfo[$indexDBHost][$indexDBName][$indexTableName]['fields_rules'][$val['Field']]['require']=false;
			}else{
				self::$tableInfo[$indexDBHost][$indexDBName][$indexTableName]['fields_rules'][$val['Field']]['require']=true;
			}
			if(strpos($val['Type'],'int')!==false || strpos($val['Type'],'float')!==false || strpos($val['Type'],'double')!==false || strpos($val['Type'],'decimal')!==false){
				self::$tableInfo[$indexDBHost][$indexDBName][$indexTableName]['fields_rules'][$val['Field']]['number']=true;
			}
			if(strpos($val['Key'],'PRI')!==false || strpos($val['Key'],'UNI')!==false){
				self::$tableInfo[$indexDBHost][$indexDBName][$indexTableName]['fields_rules'][$val['Field']]['unique']=true;
			}
			if(strpos($val['Type'],'char')===0 || strpos($val['Type'],'varchar')===0){
				$tmp=explode('(',$val['Type']);
				$length='0,'.rtrim($tmp[1],')');
				self::$tableInfo[$indexDBHost][$indexDBName][$indexTableName]['fields_rules'][$val['Field']]['length']=$length;
			}
		}
		print_r(self::$tableInfo);
	}
}